import React from 'react';
import {connect} from 'react-redux'
import {getUser} from './actions'

class App extends React.Component {

    componentDidMount() {
        if (!this.props.user.name) {
            this.props.getUser();
        }
    }

    render() {
        return <div>
            <div>name: {this.props.user.name}</div>
            <div> age: {this.props.user.age}</div>
        </div>
    }
}

const mapStateToProps = (state, ownProps)=> {
    return {
        user: state.user
    }
}

const mapDispatchToProps = (dispatch, ownProps)=> {
    return {
        getUser: ()=> {
            dispatch(getUser());
        }
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(App)
